home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / role / pinfocom_3_0.lha / Source / amiga_sound.c < prev    next >
C/C++ Source or Header  |  1992-10-22  |  7KB  |  299 lines

  1. /* amiga_sound.c
  2.  *
  3.  *  ``pinfocom'' -- a portable Infocom Inc. data file interpreter.
  4.  *  Copyright (C) 1987-1992  InfoTaskForce
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; see the file COPYING.  If not, write to the
  18.  *  Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /*
  22.  * $Header: RCS/amiga_sound.c,v 3.0 1992/10/21 16:56:19 pds Stab $
  23.  */
  24.  
  25. #ifndef _AMIGA_GLOBAL_H
  26. #include "amiga_global.h"
  27. #endif    /* !_AMIGA_GLOBAL_H */
  28.  
  29.     /* SoundInit():
  30.      *
  31.      *    Allocate resources for sound routine.
  32.      */
  33.  
  34. Bool
  35. SoundInit()
  36. {
  37.         /* Create io reply port. */
  38.  
  39.     if(SoundPort = CreatePort(NULL,0))
  40.     {
  41.             /* Create io control request. We will use it
  42.              * later to change the volume of a sound.
  43.              */
  44.  
  45.         if(SoundControlRequest = (struct IOAudio *)CreateExtIO(SoundPort,sizeof(struct IOAudio)))
  46.         {
  47.                 /* Create io request for left channel. */
  48.  
  49.             if(SoundRequestLeft = (struct IOAudio *)CreateExtIO(SoundPort,sizeof(struct IOAudio)))
  50.             {
  51.                     /* Create io request for right channel. */
  52.  
  53.                 if(SoundRequestRight = (struct IOAudio *)CreateExtIO(SoundPort,sizeof(struct IOAudio)))
  54.                 {
  55.                         /* Channel allocation map,
  56.                          * we want any two stereo
  57.                          * channels.
  58.                          */
  59.  
  60.                     STATIC UBYTE AllocationMap[] =
  61.                     {
  62.                         LEFT0F | RIGHT0F,
  63.                         LEFT0F | RIGHT1F,
  64.                         LEFT1F | RIGHT0F,
  65.                         LEFT1F | RIGHT1F
  66.                     };
  67.  
  68.                         /* Set it up for channel allocation,
  69.                          * any two stereo channels will do.
  70.                          */
  71.  
  72.                     SoundControlRequest -> ioa_Request . io_Message . mn_Node . ln_Pri    = 127;
  73.                     SoundControlRequest -> ioa_Request . io_Command                = ADCMD_ALLOCATE;
  74.                     SoundControlRequest -> ioa_Request . io_Flags                = ADIOF_NOWAIT | ADIOF_PERVOL;
  75.                     SoundControlRequest -> ioa_Data                        = AllocationMap;
  76.                     SoundControlRequest -> ioa_Length                    = sizeof(AllocationMap);
  77.  
  78.                         /* Open the device, allocating the channel on the way. */
  79.  
  80.                     if(!OpenDevice(AUDIONAME,NULL,(struct IORequest *)SoundControlRequest,NULL))
  81.                     {
  82.                             /* Copy the initial data to the
  83.                              * other audio io requests.
  84.                              */
  85.  
  86.                         CopyMem((BYTE *)SoundControlRequest,(BYTE *)SoundRequestLeft, sizeof(struct IOAudio));
  87.                         CopyMem((BYTE *)SoundControlRequest,(BYTE *)SoundRequestRight,sizeof(struct IOAudio));
  88.  
  89.                             /* Separate the channels. */
  90.  
  91.                         SoundRequestLeft  -> ioa_Request . io_Unit = (struct Unit *)((ULONG)SoundRequestLeft  -> ioa_Request . io_Unit & (LEFT0F  | LEFT1F));
  92.                         SoundRequestRight -> ioa_Request . io_Unit = (struct Unit *)((ULONG)SoundRequestRight -> ioa_Request . io_Unit & (RIGHT0F | RIGHT1F));
  93.  
  94.                             /* Return success. */
  95.  
  96.                         return(TRUE);
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.     }
  102.  
  103.         /* Clean up... */
  104.  
  105.     SoundExit();
  106.  
  107.         /* ...and return failure. */
  108.  
  109.     return(FALSE);
  110. }
  111.  
  112.     /* SoundExit():
  113.      *
  114.      *    Free resources allocated by SoundInit().
  115.      */
  116.  
  117. VOID
  118. SoundExit()
  119. {
  120.         /* Free the left channel data. */
  121.  
  122.     if(SoundRequestLeft)
  123.     {
  124.             /* Did we open the device? */
  125.  
  126.         if(SoundRequestLeft -> ioa_Request . io_Device)
  127.         {
  128.                 /* Check if the sound is still playing. */
  129.  
  130.             if(!CheckIO((struct IORequest *)SoundRequestLeft))
  131.             {
  132.                     /* Abort the request. */
  133.  
  134.                 AbortIO((struct IORequest *)SoundRequestLeft);
  135.  
  136.                     /* Wait for it to return. */
  137.  
  138.                 WaitIO((struct IORequest *)SoundRequestLeft);
  139.             }
  140.             else
  141.                 GetMsg(SoundPort);
  142.         }
  143.  
  144.             /* Free the memory allocated. */
  145.  
  146.         DeleteExtIO((struct IORequest *)SoundRequestLeft);
  147.  
  148.             /* Leave no traces. */
  149.  
  150.         SoundRequestLeft = NULL;
  151.     }
  152.  
  153.         /* Free the right channel data. */
  154.  
  155.     if(SoundRequestRight)
  156.     {
  157.             /* Did we open the device? */
  158.  
  159.         if(SoundRequestRight -> ioa_Request . io_Device)
  160.         {
  161.                 /* Check if the sound is still playing. */
  162.  
  163.             if(!CheckIO((struct IORequest *)SoundRequestRight))
  164.             {
  165.                     /* Abort the request. */
  166.  
  167.                 AbortIO((struct IORequest *)SoundRequestRight);
  168.  
  169.                     /* Wait for it to return. */
  170.  
  171.                 WaitIO((struct IORequest *)SoundRequestRight);
  172.             }
  173.             else
  174.                 GetMsg(SoundPort);
  175.         }
  176.  
  177.             /* Free the memory allocated. */
  178.  
  179.         DeleteExtIO((struct IORequest *)SoundRequestRight);
  180.  
  181.             /* Leave no traces. */
  182.  
  183.         SoundRequestRight = NULL;
  184.     }
  185.  
  186.         /* Free sound control request. */
  187.  
  188.     if(SoundControlRequest)
  189.     {
  190.             /* Close the device, free any allocated channels. */
  191.  
  192.         if(SoundControlRequest -> ioa_Request . io_Device)
  193.             CloseDevice((struct IORequest *)SoundControlRequest);
  194.  
  195.             /* Free the memory allocated. */
  196.  
  197.         DeleteExtIO((struct IORequest *)SoundControlRequest);
  198.  
  199.             /* Leave no traces. */
  200.  
  201.         SoundControlRequest = NULL;
  202.     }
  203.  
  204.         /* Free sound io reply port. */
  205.  
  206.     if(SoundPort)
  207.     {
  208.             /* Delete it. */
  209.  
  210.         DeletePort(SoundPort);
  211.  
  212.             /* Leave no traces. */
  213.  
  214.         SoundPort = NULL;
  215.     }
  216.  
  217.         /* Free previously allocated sound data. */
  218.  
  219.     if(SoundData && SoundLength)
  220.     {
  221.             /* Free it. */
  222.  
  223.         FreeMem(SoundData,SoundLength);
  224.  
  225.             /* Leave no traces. */
  226.  
  227.         SoundData    = NULL;
  228.         SoundLength    = 0;
  229.     }
  230.  
  231.         /* Clear current sound number. */
  232.  
  233.     SoundNumber = -1;
  234. }
  235.  
  236.     /* SoundAbort():
  237.      *
  238.      *    Abort any currently playing sound and wait for
  239.      *    both IORequests to return.
  240.      */
  241.  
  242. VOID
  243. SoundAbort()
  244. {
  245.         /* Abort sound playing on the left channel. */
  246.  
  247.     if(!CheckIO((struct IORequest *)SoundRequestLeft))
  248.         AbortIO((struct IORequest *)SoundRequestLeft);
  249.  
  250.         /* Abort sound playing on the right channel. */
  251.  
  252.     if(!CheckIO((struct IORequest *)SoundRequestRight))
  253.         AbortIO((struct IORequest *)SoundRequestRight);
  254.  
  255.         /* Wait for the request to return. */
  256.  
  257.     WaitIO((struct IORequest *)SoundRequestLeft);
  258.  
  259.         /* Wait for the request to return. */
  260.  
  261.     WaitIO((struct IORequest *)SoundRequestRight);
  262. }
  263.  
  264.     /* SoundStop():
  265.      *
  266.      *    Stop sound from getting played (somewhat equivalent to ^S).
  267.      */
  268.  
  269. VOID
  270. SoundStop()
  271. {
  272.         /* Fill in the command. */
  273.  
  274.     SoundControlRequest -> ioa_Request . io_Command = CMD_STOP;
  275.  
  276.         /* Send it off. */
  277.  
  278.     SendIO((struct IORequest *)SoundControlRequest);
  279.     WaitIO((struct IORequest *)SoundControlRequest);
  280. }
  281.  
  282.     /* SoundStart():
  283.      *
  284.      *    Restart any queued sound (somewhat equivalent to ^Q).
  285.      */
  286.  
  287. VOID
  288. SoundStart()
  289. {
  290.         /* Fill in the command. */
  291.  
  292.     SoundControlRequest -> ioa_Request . io_Command = CMD_START;
  293.  
  294.         /* Send it off. */
  295.  
  296.     SendIO((struct IORequest *)SoundControlRequest);
  297.     WaitIO((struct IORequest *)SoundControlRequest);
  298. }
  299.